package com.sunmnet.j2ee.action.powerdb.designer;

import com.sunmnet.j2ee.action.BaseNewAction;
import com.sunmnet.j2ee.bean.pub.action.JsonResult;
import com.sunmnet.j2ee.bean.pub.action.PageResult;
import com.sunmnet.j2ee.core.pub.QueryFilter;
import com.sunmnet.j2ee.core.pub.annotation.MethodName;
import com.sunmnet.j2ee.entity.powerdb.designer.Database;
import com.sunmnet.j2ee.service.BaseService;
import com.sunmnet.j2ee.service.powerdb.designer.DatabaseService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@Controller("/powerdb/designer/database")
@RequestMapping("/powerdb/designer/database")
public class DatabaseAction extends BaseNewAction<Database, Long> {
	/**
	 * log4j2日志 *
	 */

	private static final Logger LOG = LogManager.getLogger(DatabaseAction.class.getName());

	@Override
	@Resource(name = "/powerdb/designer/databaseService")
	public void setService(BaseService<Database, Long> service) {
		super.service = service;
	}

	/**
	 * 批量查询
	 *
	 * @param request
	 * @return JsonResult
	 */

	@MethodName(name = "批量查询")
	@RequestMapping(value = "/", method = RequestMethod.PATCH)
	@ResponseBody
	public JsonResult find(HttpServletRequest request) {
		QueryFilter filter = new QueryFilter(request);
		return super.find(filter);
	}


	/**
	 * 批量查询
	 *
	 * @param request
	 * @return JsonResult
	 */

	@MethodName(name = "批量查询")
	@RequestMapping(value = "/pages", method = RequestMethod.PATCH)
	@ResponseBody
	public PageResult findPages(HttpServletRequest request) {
		QueryFilter filter = new QueryFilter(request);
		return super.findPages(filter);
	}


	/**
	 * 查询
	 *
	 * @param id
	 * @return JsonResult
	 */

	@MethodName(name = "对象查询")
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	public JsonResult get(@PathVariable("id") long id, HttpServletRequest request) {
		return super.get(id);
	}


	/**
	 * 新增
	 *
	 * @param obj
	 * @return JsonResult
	 */

	@MethodName(name = "新增")
	@RequestMapping(value = "/", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult add(Database obj) {
		if (((DatabaseService) service).checkRepeat(obj) == 1) {
			JsonResult jsonResult = new JsonResult(false);
			jsonResult.setObj(obj);
			jsonResult.setMsg("数据库【" + obj.getName() + "】已经存在，请重新命名！");
			return jsonResult;
		}
		return super.add(obj);
	}


	/**
	 * 更新
	 *
	 * @param obj
	 * @return JsonResult
	 */

	@MethodName(name = "修改")
	@RequestMapping(value = "/", method = RequestMethod.PUT)
	@ResponseBody
	public JsonResult update(Database obj) {
		if (((DatabaseService) service).checkRepeat(obj) == 1) {
			JsonResult jsonResult = new JsonResult(false);
			jsonResult.setObj(obj);
			jsonResult.setMsg("数据库【" + obj.getName() + "】已经存在，请重新命名！");
			return jsonResult;
		}
		return super.update(obj);
	}


	/**
	 * 删除
	 *
	 * @param id
	 * @return JsonResult
	 */

	@MethodName(name = "删除")
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public JsonResult delete(@PathVariable("id") long id) {
		return super.delete(id);
	}


	/**
	 * 批量删除
	 *
	 * @param ids 一个或多个id "1,2,3"
	 * @return JsonResult
	 */

	@MethodName(name = "批量删除")
	@RequestMapping(value = "/batch", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult batchDelete(@RequestParam(value = "ids", required = true) String ids) {
		return super.delete(ids);
	}

	/**
	 * 查询数据库信息
	 *
	 * @param request
	 * @return JsonResult
	 */
	@MethodName(name = "查询数据库信息")
	@RequestMapping(value = "/findDatabase", method = RequestMethod.PATCH)
	@ResponseBody
	public JsonResult findDatabase(HttpServletRequest request) {

		JsonResult json = new JsonResult(true);
		QueryFilter filter = new QueryFilter(request);
		try {
			json.setObj(((DatabaseService) service).getDatabase(filter));
			json.setMsg("查询数据库成功");
		} catch (Exception e) {
			setErrMsg("查询数据库失败", LOG, json, e);
		}
		return json;
	}

}


